package org.dromara.third.convert.riskMeasure.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.RiskMeasure;
import org.dromara.system.mapper.RiskMeasureMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.riskMeasure.dto.RiskMeasureDTO;
import org.dromara.third.convert.riskMeasure.validator.RiskMeasureValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertRiskMeasureController {

    @Autowired
    private RiskMeasureMapper riskMeasureMapper;

    @PostMapping("/riskMeasure")
    @Transactional
    public R<Void> addRiskMeasure(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        RiskMeasureDTO riskMeasureDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            riskMeasureDTO = JSONObject.parseObject(decrypt, RiskMeasureDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, riskMeasureDTO, new RiskMeasureValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        RiskMeasure riskMeasure = assembleRiskMeasureData(orgCode, riskMeasureDTO);
        int count = riskMeasureMapper.countWithOutDelFlag(riskMeasure.getId());
        if (count == 0) {
            riskMeasureMapper.insert(riskMeasure);
        } else {
            riskMeasureMapper.updateWithOutDelFlag(riskMeasure);
        }
        return R.ok();
    }

    private RiskMeasure assembleRiskMeasureData(String orgCode, RiskMeasureDTO dto) {
        RiskMeasure riskMeasure = new RiskMeasure();
        try {
            riskMeasure.setId(dto.getId());
            riskMeasure.setRiskEventId(dto.getRiskEventId());
            riskMeasure.setClassify1(dto.getClassify1());
            riskMeasure.setClassify2(dto.getClassify2());
            riskMeasure.setClassify3(dto.getClassify3());
            riskMeasure.setDataSrc(dto.getDataSrc());
            riskMeasure.setRiskMeasureDesc(dto.getRiskMeasureDesc());
            riskMeasure.setTroubleShootContent(dto.getTroubleShootContent());
            riskMeasure.setDelFlag(Long.parseLong(dto.getDelFlag()));
            riskMeasure.setCreateDept(Long.parseLong(dto.getCreateDept()));
            riskMeasure.setCreateBy(Long.parseLong(dto.getCreateBy()));
            riskMeasure.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            riskMeasure.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            riskMeasure.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            riskMeasure.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装管控措施信息异常");
        }
        return riskMeasure;
    }
}
